package moonhy.c2dm_client;

import java.util.ArrayList;

import moonhy.c2dm_client.dw.MemberData;
import moonhy.c2dm_client.util.JSONSharedPreferences;
import moonhy.c2dm_client.util.httpConnectionManager;

import org.json.JSONArray;
import org.json.JSONObject;
import org.taptwo.android.widget.TitleFlowIndicator;
import org.taptwo.android.widget.ViewFlow;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Parcelable;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.widget.Adapter;
import android.widget.LinearLayout;
import android.widget.TextView;


public class MemberListActivity extends Activity{
	ArrayList<MemberData> md_array = new ArrayList<MemberData>();
	private ViewFlow viewFlow;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.member_list_main);
		
		new initPage(this).execute();
	}
	
	private Object setData(){
		//String readNotice = readJSON("http://moonhy.mireene.com/kline/notice/notice_list.php");
		
		ArrayList<MemberData> md_array = new ArrayList<MemberData>();
		
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
		int SAVED_MEMBER_CODE = prefs.getInt("SAVED_MEMBER_CODE", 0);
		int CUR_MEMBER_CODE = prefs.getInt("CUR_MEMBER_CODE", 0);
		
		try {
			JSONArray json_result = JSONSharedPreferences.loadJSONArray(this, "gline", "memberInfo");
			
			Log.d("C2DM", json_result.length() + " ----------- " + json_result + " ----------- ");
			
			if(json_result.isNull(0) || SAVED_MEMBER_CODE != CUR_MEMBER_CODE){
				httpConnectionManager hcm = new httpConnectionManager(this);
				
				JSONObject json = new JSONObject(hcm.readJSON(getString(R.string.SERVER_URL) + "members/member_list.php",null));
				Log.d("C2DM", getString(R.string.SERVER_URL) + "members/member_list.php");
				
				json_result = json.getJSONArray("data_result");
				JSONSharedPreferences.saveJSONArray(this, "gline", "memberInfo", json_result);
				
				Editor edit = prefs.edit();
				edit.putInt("SAVED_MEMBER_CODE", prefs.getInt("CUR_MEMBER_CODE", 0));
				edit.commit();
			}
			
			for (int i = 0; i < json_result.length(); i++) {
				JSONObject jsonObject = json_result.getJSONObject(i);
				MemberData tmp_nd = new MemberData();
				tmp_nd.setMemberName(jsonObject.getString("mem_name"));
				tmp_nd.setMemberPhone(jsonObject.getString("mem_phone"));
				tmp_nd.setMemberEmail(jsonObject.getString("mem_email"));
				tmp_nd.setMemberBirth(jsonObject.getString("mem_birth"));
				tmp_nd.setMemberDesc(jsonObject.getString("mem_desc"));
				md_array.add(i, tmp_nd);
				
				//Log.d("C2DM", memberName[i] + " --------------");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return md_array;
	}
	
	
	private class initPage extends AsyncTask<Void, Object, Object>{
		Context context;
		initPage(Context context){
			this.context = context;
		}
		protected void onPreExecute(){
		}

		protected Object doInBackground(Void... unused){
    		return setData();
        }

        protected void onPostExecute(Object result){
        	viewFlow = (ViewFlow) findViewById(R.id.viewflow);
    		MemberListAsyncAdapter adapter = new MemberListAsyncAdapter(context, result);
    		viewFlow.setAdapter(adapter);
    		
    		TitleFlowIndicator indicator = (TitleFlowIndicator) findViewById(R.id.viewflowindic);
    		indicator.setTitleProvider(adapter);
    		viewFlow.setFlowIndicator(indicator);
        }
	}
}

